import 'dart:convert';

import 'package:lib_database/lib_database.dart';
import 'package:lib_network/http_manager.dart';
import 'package:lib_network/lib_network.dart';
import 'package:lib_util/log/log.dart';

import '../network/http_api.dart';

class HttpTradeList {
  // 获取交易列表
  Future<List<TransactionBean>> getTokenTradingList(Token token, {int page = 1, int limit = 100}) async {
    try {
      Chain? chain = token.getChain();
      Map<String, dynamic> params = {
        'address': token.getWalletInfo()!.address.toLowerCase(),
        "chain_id": chain!.evmInfo!.chainId.toString(),
        "wallet_id": 0,
        "symbol": token.contractType == 'native' ? token.symbol : '',
        'contract_address': token.contract.toLowerCase(),
        'page': {'page': page, 'limit': limit}
      };
      var result = await HttpManager().post(API.getTokenTradingList, params);
      if (result?.code != HttpConfig().successCode) {
        throw Exception('Error');
      }
      List tradingList = result!.data['trading_list'];

      List<TransactionBean> arr =
          tradingList.map((e) => TransactionBean.fromJson(e, decimal: token.decimals)).toList(growable: false);
      return arr;
    } catch (e) {
      print('getTokenTradingList :::::: $e');
      return [];
    }
  }
}
